package com.atguigu.juc.atomic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: CountDownLatch 代替 Thread.sleep()
 * @author： yeswater
 * @create： 2024/5/30
 */

public class CountDownLatchDemo {
	
	private static CountDownLatch countDownLatch = new CountDownLatch(50);
	
	private static AtomicInteger atomicInteger = new AtomicInteger(0);
	
	public static void main(String[] args) throws InterruptedException {
		for (int i = 0; i < 50; i++) {
			new Thread(() -> {
				try {
					for (int i1 = 0; i1 < 10; i1++) {
						atomicInteger.getAndIncrement();
					}
				} finally {
					countDownLatch.countDown();
				}
			}, "t" + i).start();
		}
		
		// Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted.
		countDownLatch.await(); // 使当前线程等待，直到锁存器倒计时为零，除非线程被中断。
		
		System.out.println("result:" + atomicInteger.get());
	}
	
}
